/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import common.Constant;
import dao.ProductDAO;
import model.ProductModel;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
 *
 * @author ThanhToan
 */
public class ProductController extends HttpServlet {
    private String URL="";
    private void mapParameterToModel(HttpServletRequest req
			,HttpServletResponse res
				,ProductModel proModel) {
        if (req.getParameter("txtProName") != null) {
			proModel.setProName(req.getParameter("txtProName").trim()); }
        if (req.getParameter("txtProDes") != null) {
			proModel.setProDes(req.getParameter("txtProDes").trim()); }
        if (req.getParameter("txtPrice") != null) {
			proModel.setPrice(req.getParameter("txtPrice").trim()); }
        if (req.getParameter("fileImage") != null) {
			proModel.setImage(req.getParameter("fileImage").trim()); }
        proModel.setCateID(req.getParameter("cboCategories").trim());
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if(request.getParameter("proDetail") != null)
	{
            productDetails(request, response);
	}
        if(request.getParameter("search") != null)
	{
            searchProduct(request, response);
	}
        if(request.getParameter("sort") != null)
	{
            sortProduct(request, response);
	}
    }

    private void productDetails(HttpServletRequest request
			,HttpServletResponse response)throws ServletException, IOException {
        String actionType = request.getParameter("actionType");
        ProductModel proModel = new ProductModel();
        mapParameterToModel(request, response, proModel);
        if(actionType.equals("CreateNew")){
            if(proModel.validateProduct()){ //validate
                if(ProductDAO.insertProduct(proModel)){
                    URL = "//productDetails.jsp?msg='CreateNew'";
                }
                else{ //validate err
                    URL = "//productDetails.jsp?err='Validate Error'";
                }
            }
        }
        if(actionType.equals("Update")){
            if(proModel.validateProduct()){ //validate
                if(ProductDAO.updateProduct(proModel)){
                    URL = "//productDetails.jsp?msg='Update'";
                }
                else{ //validate err
                    URL = "//productDetails.jsp?err='Validate Error'";
                }
            }
        }
        RequestDispatcher rd = request.getRequestDispatcher(URL);
        request.setAttribute(Constant.PRODUCT_INFO, proModel);
        rd.forward(request, response);
    }

    private void searchProduct(HttpServletRequest request
			,HttpServletResponse response)throws ServletException, IOException {
        String cateID = request.getParameter("cboCategories").trim().toString();
        String proName = request.getParameter("txtProName").trim().toString();
        ArrayList<ProductModel> list = new ArrayList<ProductModel>();
        list = ProductDAO.getFindProduct(cateID, proName);
        URL = "searchProduct.jsp";
        RequestDispatcher rd = request.getRequestDispatcher(URL);
        request.setAttribute(Constant.PRODUCT_LIST, list);
        rd.forward(request, response);
    }

    private void sortProduct(HttpServletRequest request
			,HttpServletResponse response)throws ServletException, IOException {
        String cateID = request.getParameter("cboCategories").trim().toString();
        URL = "product.jsp?cateID="+cateID;
        RequestDispatcher rd = request.getRequestDispatcher(URL);
        rd.forward(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}